<?php
date_default_timezone_set('PRC');
//phpinfo();

$url='https://www.douban.com/search?cat=1002&q=%E8%AF%9B%E4%BB%99&v=1568475132';
//$url = 'https://movie.douban.com/subject_search?search_text=%E4%B8%83%E4%BB%99%E5%A5%B3&cat=1002';
//$url = 'https://movie.douban.com/j/subject_suggest?q=%E4%B8%83%E4%BB%99%E5%A5%B3';
//$url = 'http://web.chacuo.net/netproxycheck';

$proxy_protocol='http';
$proxy_ip='183.220.145.3';
$proxy_port='80';
echo ff_file_get_contents($url,30,'','',$proxy_protocol,$proxy_ip,$proxy_port);
//echo ff_file_get_contents($url,30,'','');


function ff_file_get_contents($url, $timeout=10, $referer='', $post_data='',$proxy_protocol='http',$proxy_ip='',$proxy_port=0){

    $http = parse_url($url);
    if(function_exists('curl_init')){
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_HEADER, 0);
        curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt ($ch, CURLOPT_REFERER, $referer);
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);

        $headers = array(
            "Cache-Control: no-cache",
            "Postman-Token: 17957144-bad2-42f2-99b9-50d2f313c37f",
            "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
        );

        curl_setopt ($ch, CURLOPT_HTTPHEADER , $headers);

        //post
        if($post_data){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }else{
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        }

        //https
        if($http['scheme'] == 'https'){
            curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }

        if($proxy_ip!=''&&$proxy_port!=0){
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
            curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
            curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
            if($proxy_protocol=='http'){
                curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
            }else{
                curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
            }
        }

        $content = curl_exec($ch);
        $curlerr = curl_error($ch);

        curl_close($ch);
        if($content){
            var_dump('curl is success<br>');
            return $content;
        }else{
            var_dump('测试 curl_exec 方式，curl_exec 失败：== ：'.$curlerr.' proxyIP = '.$proxy_ip.':'.$proxy_port );
        }
    }else{
        var_dump('不支持 curl_init ');
    }

    $ctx = stream_context_create(array($http['scheme']=>array('timeout'=>$timeout)));
    if($proxy_ip!=''&&$proxy_port!=0){
        $ctx = stream_context_create(array($http['scheme']=>array('timeout'=>$timeout,'proxy'=>'tcp://'.$proxy_ip.':'.$proxy_port,'request_fulluri' => True )));
    }
    $content = @file_get_contents($url, 0, $ctx);
    if($content){
        var_dump('stream_context 方式请求成功::'.substr($content,0,300).'<br>');
        return $content;
    }else{
        var_dump('stream_context 方式请求失败！'.$url.'<br>');
    }
    return false;
}